<?php
include_once 'aliyun-php-sdk-core/Config.php'; // 这里开头千万不要写 ./aliyun-php-sdk-core
use Sms\Request\V20160927 as Sms;

/**
 * Class SendSms
 * 阿里云发送短信类
 * @author mr.zhou zhouwei9280@gmail.com
 */
final class SendSms {

    private $acccessKeyId; // 访问密钥
    private $acccessSecret; // 访问secret
    private $signName; // 签名
    private $templateCode;  // 模板名称
    private $paramString; // 模板变量,数字要转换成字符串
    private $sendPhone; // 要发送到.... (手机号),多个手机可以用 ',' 分割

    /**
     * SendSms constructor.
     * @param $keyId  // 密钥
     * @param $secret // Secret
     * @param $signName //签名
     * @param $tempCode // 模板名称
     * @param $paramStr // 模板变量
     */
    public function __construct($keyId,$secret,$signName,$tempCode,$paramStr,$phone)
    {
        if(!$keyId || !$secret || !$signName || !$tempCode || !$paramStr || !$phone){
            return "请传入必要的参数";
        }
        $this->setKeyId($keyId);
        $this->setSecret($secret);
        $this->setSignName($signName);
        $this->setTempCode($tempCode);
        $this->setParamStr($paramStr);
        $this->setSendPhone($phone);
    }

    /**
     * [SendMsg 发送短信方法]
     * @author mr.zhou <zhouwei9280@gmail.com>
     */
    public function SendMsg()
    {
        $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $this->getKeyId(), $this->getSecret());
        $client = new DefaultAcsClient($iClientProfile);
        $request = new Sms\SingleSendSmsRequest();
        $request->setSignName($this->getSignName());/*签名名称*/
        $request->setTemplateCode($this->getTempCode());/*模板code*/
        $request->setRecNum($this->getSendPhone());/*目标手机号*/
        $request->setParamString($this->getParamStr());/*模板变量，数字一定要转换为字符串*/
        $response = $client->getAcsResponse($request);
        if($response->Model){
            return '发送成功';
        }else{
            return $response->ErrCode;
        }
    }
    // ---
    public function getKeyId()
    {
        return $this->acccessKeyId;
    }

    public function setKeyId($keyId)
    {
        $this->acccessKeyId = $keyId;
    }
    // ----
    public function getSecret()
    {
        return $this->acccessSecret;
    }

    public function setSecret($secret)
    {
        $this->acccessSecret = $secret;
    }
    // ----
    public function getSignName()
    {
        return $this->signName;
    }

    public function setSignName($signName)
    {
        $this->signName = $signName;
    }
    // ----
    public function getTempCode()
    {
        return $this->templateCode;
    }

    public function setTempCode($tempCode)
    {
        $this->templateCode = $tempCode;
    }

    // ----
    public function getParamStr()
    {
        return $this->paramString;
    }

    public function setParamStr($paramStr)
    {
        $this->paramString = $paramStr;
    }

    // ----
    public function getSendPhone()
    {
        return $this->sendPhone;
    }

    public function setSendPhone($sendPhone)
    {
        $this->sendPhone =$sendPhone;
    }
}

